package com.example.cdctest.controller;

import com.example.cdctest.pojo.Category;
import com.example.cdctest.pojo.Question;
import com.example.cdctest.service.CategoryService;
import com.example.cdctest.service.QuestionService;
import com.example.cdctest.service.QuestionTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/services")
public class SpecialExercisesController {
//    int num=0;
    @Resource
    private QuestionTypeService questionTypeService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private QuestionService questionService;
    @RequestMapping("question_type")
    public ModelAndView question_type() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("questionType",questionTypeService.specialExercises());
        modelAndView.addObject("category",categoryService.specialExercises());
        return modelAndView;
    }
    @RequestMapping("question_type_examination")
    public ModelAndView question_type_examination(Integer questionTypeId,HttpSession session)
    {
        ModelAndView modelAndView=new ModelAndView();
        List<Question> questions= questionService.selectQuestionByQuestionTypeid(questionTypeId);
        modelAndView.addObject("questionTypeId",questionTypeId);
        session.setAttribute("num",0);

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination");
        return modelAndView;
    }
    @RequestMapping("category_examination")
    public ModelAndView category_examination(Integer categoryId,HttpSession session, HttpServletRequest request)
    {
        ModelAndView modelAndView=new ModelAndView();
        List<Question> questions= questionService.selectQuestionByCategoryid(categoryId);
        modelAndView.addObject("categoryId",categoryId);
        session.setAttribute("num",0);

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/category_examination");
        return modelAndView;
    }
    @RequestMapping("xiayiti")
    @ResponseBody
    public ModelAndView xiayiti(Integer categoryId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByCategoryid(categoryId);
        Integer num= (Integer) session.getAttribute("num");
        if (num<questions.size()-1)
        {
            num=num+1;
        }
        else {
            num=num;
        }
        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }
    @RequestMapping("shangyiti")
    @ResponseBody
    public ModelAndView shangyiti(Integer categoryId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByCategoryid(categoryId);
        Integer num= (Integer) session.getAttribute("num");
        if (num>0)
        {
            num=num-1;
        }
        else
        {
            num=num;
        }
        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }

    @RequestMapping("xiayiti1")
    @ResponseBody
    public ModelAndView xiayiti1(Integer questionTypeId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByQuestionTypeid(questionTypeId);
        Integer num= (Integer) session.getAttribute("num");
        if (num<questions.size()-1)
        {
            num=num+1;
        }
        else {
            num=num;
        }
        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }
    @RequestMapping("shangyiti1")
    @ResponseBody
    public ModelAndView shangyiti1(Integer questionTypeId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByQuestionTypeid(questionTypeId);
        Integer num= (Integer) session.getAttribute("num");
        if (num>0)
        {
            num=num-1;
        }
        else
        {
            num=num;
        }
        session.setAttribute("num",num);
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }

    @RequestMapping("answersPaperq")
    @ResponseBody
    public ModelAndView answersPaperq(Integer count,Integer questionTypeId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByQuestionTypeid(questionTypeId);
        Integer num= (Integer) session.getAttribute("num");
        num=count-1;
        session.setAttribute("num",num);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }
    @RequestMapping("answersPaperc")
    @ResponseBody
    public ModelAndView answersPaperc(Integer count,Integer categoryId,HttpSession session)
    {
        List<Question> questions= questionService.selectQuestionByCategoryid(categoryId);
        Integer num= (Integer) session.getAttribute("num");
        num=count-1;
        session.setAttribute("num",num);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("questions",questions);
        modelAndView.setViewName("services/question_type_examination_manage");
        return modelAndView;
    }
}
